package com.hubujun.driverlicense.db;

import android.content.ContentValues;
import android.database.Cursor;

import com.hubujun.driverlicense.model.QuestionModel;
import com.hubujun.driverlicense.util.Ln;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/5/28 0028.
 */
public class QuestionStorage {
    public static class QuestionStorageBuildTable implements BuildTable {

        @Override
        public String getName() {
            return TABLE;
        }

        @Override
        public String[] onCreate() {
            return new String[]{"CREATE TABLE IF NOT EXISTS " + TABLE + " (" + ID + " INTEGER UNIQUE, " + QUESTION + " TEXT, " + ANSWER + " TEXT, " + ITEM1 + " TEXT, " + ITEM2 + " TEXT, " + ITEM3 + " TEXT, " + ITEM4 + " TEXT," + EXPLAINS + " TEXT, " + URL + " TEXT)"};
        }

        @Override
        public void onUpdate(SqliteDB db, int oldVersion, int newVersion) {
        }

    }

    public static final String TABLE = "question";

    public static final String ID = "id";
    public static final String QUESTION = "question";
    public static final String ANSWER = "answer";
    public static final String ITEM1 = "item1";
    public static final String ITEM2 = "item2";
    public static final String ITEM3 = "item3";
    public static final String ITEM4 = "item4";
    public static final String EXPLAINS = "explains";
    public static final String URL = "url";
    private SqliteDB mSqlDB;

    public QuestionStorage(SqliteDB sqlDB) {
        this.mSqlDB = sqlDB;
    }

    public void addQuestionList(List<QuestionModel> questionModels) {
        int key = mSqlDB.beginTransaction();
        addQuestionListNoTran(questionModels);
        mSqlDB.setTransactionSuccessful(key);
        mSqlDB.endTransaction(key);
    }

    public void addQuestionListNoTran(List<QuestionModel> questionModels) {
        if (questionModels == null) {
            return;
        }
        for (QuestionModel questionModel : questionModels) {
            addQuestion(questionModel);
        }
    }


    public void addQuestion(QuestionModel questionModel) {
        ContentValues values = new ContentValues();
        values.put(ID, questionModel.id);
        values.put(QUESTION, questionModel.question);
        values.put(ANSWER, questionModel.answer);
        values.put(ITEM1, questionModel.item1);
        values.put(ITEM2, questionModel.item2);
        values.put(ITEM3, questionModel.item3);
        values.put(ITEM4, questionModel.item4);
        values.put(EXPLAINS, questionModel.explains);
        values.put(URL, questionModel.url);
        mSqlDB.replace(TABLE, null, values);
    }

    public List<QuestionModel> getQuestionList() {
        List<QuestionModel> list = new ArrayList<QuestionModel>();
        Cursor c = mSqlDB.query(TABLE, null, null, null, null);
        if (c != null) {
            try {
                while (c.moveToNext()) {
                    QuestionModel q = new QuestionModel();
                    fillData(c, q);
                    list.add(q);
                }
            } catch (Exception e) {
                Ln.e(e);
            } finally {
                c.close();
            }
        }
        return list;
    }

    private void fillData(Cursor c, QuestionModel questionModel) {
        questionModel.id = c.getInt(c.getColumnIndex(ID));
        questionModel.question = c.getString(c.getColumnIndex(QUESTION));
        questionModel.answer = c.getString(c.getColumnIndex(ANSWER));
        questionModel.item1 = c.getString(c.getColumnIndex(ITEM1));
        questionModel.item2 = c.getString(c.getColumnIndex(ITEM2));
        questionModel.item3 = c.getString(c.getColumnIndex(ITEM3));
        questionModel.item4 = c.getString(c.getColumnIndex(ITEM4));
        questionModel.explains = c.getString(c.getColumnIndex(EXPLAINS));
        questionModel.url = c.getString(c.getColumnIndex(URL));
    }
}
